package com.sdry.web.controller.workplace;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hacker.base.BaseController;
import com.sdry.model.llm.User;
import com.sdry.model.workplace.MousseBundleSample;
import com.sdry.model.workplace.MousseBundleSampleExample;
import com.sdry.model.workplace.MousseSample;
import com.sdry.model.workplace.MousseSampleExample;
import com.sdry.model.workplace.MousseSampleExample.Criteria;
import com.sdry.service.workplace.MousseBundleSampleServise;
import com.sdry.service.workplace.MousseSampleServise;
import com.sdry.utils.ResponseUtil;


/**
 * @ClassName: mousseSampleController
 * @Description: 膜丝留样
 * @author tdd
 * @date 2018年8月29日
 * @version V1.0
 */
@Controller
@RequestMapping("/mousseSample")
public class MousseSampleController extends BaseController{

    @Resource
    private MousseSampleServise mousseSampleServise;

    @Resource
    private MousseBundleSampleServise mousseBundleSampleServise;
    
    
    
    public int editBundleSample( MousseBundleSample mousseBundleSample){
    	int affact = 0;
        //修改
    	affact = mousseBundleSampleServise.updateByPrimaryKeySelective(mousseBundleSample);
        return affact;
    }
    /**
     * 添加膜丝留样
     * @param response 响应
     * @param mousseSample 膜丝留样实体
     * @throws Exception 异常
     */
    @RequestMapping("/insertmousseSample")
    public void insertmousseSample(HttpServletRequest request, HttpServletResponse response, MousseSample mousseSample) throws Exception {
    	int affact = 0;

    	MousseBundleSample mousseBundleSample = new MousseBundleSample();
    	mousseBundleSample.setMbsid(Long.valueOf(mousseSample.getMbcode()));
    	mousseBundleSample.setMbsremark("膜丝绑定");
		if (editBundleSample(mousseBundleSample) > 0) {
			MousseSampleExample example = new MousseSampleExample();
			Criteria createCriteria =  example.createCriteria();
			//名称查询
			if(mousseSample.getMbcode() != null && mousseSample.getMbcode() != "") {
				createCriteria.andMbcodeEqualTo(mousseSample.getMbcode());
			}
			List<MousseSample> mousseSamples = mousseSampleServise.selectByExample(example);
			if(mousseSamples.size() == 0){
				
				//判断session
	      		HttpSession session = request.getSession();
	      		// 从session中取出用户身份信息
	      		User use = (User) session.getAttribute("currentUser");
	      		//当前时间
	            SimpleDateFormat   sDateFormat   =   new   SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
	            String   date   =   sDateFormat.format(new Date());
	            mousseSample.setMsoperator(use.getUserName());
	            mousseSample.setMstime(date);
				
				//抽检状态    0未抽检
				mousseSample.setMsremark("0");
				//添加
				affact = mousseSampleServise.insertSelective(mousseSample);
			}
			
		}
        ResponseUtil.write(response, affact);
    }

    /**
     * 带条件的分页查询膜丝留样信息
     * @param request 请求
     * @param response 响应
     * @param rows 每页记录数
     * @param page 页数
     * @param mousseSample 膜丝留样信息实体
     * @throws Exception 异常
     */
    @RequestMapping("/selectmousseSampleList")
    public void selectmousseSampleList(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(value = "page",required = false)int page,
                                   @RequestParam(value = "rows",required = false)int rows,
                                   MousseSample mousseSample) throws Exception {

    	PageHelper.startPage(page, rows);

        MousseSampleExample example = new MousseSampleExample();
        example.setOrderByClause("mstime desc");
        Criteria createCriteria =  example.createCriteria();
    	//名称模糊
        if(mousseSample.getMbcode() != null && mousseSample.getMbcode() != "") {
    		createCriteria.andMbcodeLike("%"+mousseSample.getMbcode()+"%");
    	}
        //名称模糊
        if(mousseSample.getMscode() != null && mousseSample.getMscode() != "") {
        	createCriteria.andMscodeLike("%"+mousseSample.getMscode()+"%");
        }
        //抽检状态    0未抽检   1抽检
        createCriteria.andMsremarkEqualTo("0");
		List<MousseSample> list = mousseSampleServise.selectByExample(example);
		
		PageInfo<MousseSample> info = new PageInfo<MousseSample>(list);
    	
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("total", info.getTotal());
        jsonObject.put("rows", info.getList());
        ResponseUtil.write(response, jsonObject);
    }

    /**
     * 根据条件查询膜丝留样信息
     * @param response 响应
     * @param mousseSample 条件
     * @throws Exception 异常
     */
    @RequestMapping("/selectmousseSampleByExample")
    public void selectmousseSampleByExample(HttpServletResponse response, MousseSample mousseSample) throws Exception {
        MousseSampleExample example = new MousseSampleExample();
        Criteria createCriteria =  example.createCriteria();
        //名称查询
        if(mousseSample.getMbcode() != null && mousseSample.getMbcode() != "") {
            createCriteria.andMbcodeEqualTo(mousseSample.getMbcode());
        }
        if(mousseSample.getMscode() != null && mousseSample.getMscode() != "") {
        	createCriteria.andMscodeEqualTo(mousseSample.getMscode());
        }
        //抽检状态    0未抽检   1抽检
        createCriteria.andMsremarkEqualTo("0");
        List<MousseSample> mousseSamples = mousseSampleServise.selectByExample(example);
        //将对象转化为json数组
        JSONArray json = JSONArray.fromObject(mousseSamples);
        ResponseUtil.write(response, json);
    }
    /**
     * 根据id查询膜丝留样信息
     * @param response 响应
     * @param id 膜丝留样id
     * @throws Exception 异常
     */
    @RequestMapping("/selectmousseSampleById")
    public void selectmousseSampleById(HttpServletResponse response, Long id) throws Exception {

        MousseSample mousseSample = mousseSampleServise.selectByPrimaryKey(id);
        //将对象转化为json
        JSONObject json = JSONObject.fromObject(mousseSample);
        ResponseUtil.write(response, json);
    }

    /**
     * 修改膜丝留样信息
     * @param response 响应
     * @param mousseSample 膜丝留样信息
     * @throws Exception 异常
     */
    @RequestMapping("/editmousseSample")
    public void editmousseSample( HttpServletResponse response,
                              MousseSample mousseSample) throws Exception {

        int affct = 0;

        MousseSampleExample example = new MousseSampleExample();
        Criteria createCriteria =  example.createCriteria();
        //名称查询
        if(mousseSample.getMbcode() != null && mousseSample.getMbcode() != "") {
            createCriteria.andMbcodeEqualTo(mousseSample.getMbcode());
        }
        List<MousseSample> mousseSamples = mousseSampleServise.selectByExample(example);
        if(mousseSamples.size() == 0){
            //修改
            affct = mousseSampleServise.updateByPrimaryKeySelective(mousseSample);
        }else if(mousseSamples.size() == 1 && mousseSamples.get(0).getMsid().equals(mousseSample.getMsid())){
            affct = mousseSampleServise.updateByPrimaryKeySelective(mousseSample);
        }
        ResponseUtil.write(response, affct);
    }

    /**
     * 批量删除
     * @param response 响应
     * @param ids 膜丝留样id
     * @throws Exception 异常
     */
    @RequestMapping("/deletemousseSample")
    public void deletemousseSample( HttpServletResponse response, String ids) throws Exception {

        int affact = mousseSampleServise.deleteByPrimaryKeys(ids);
        ResponseUtil.write(response, affact);
    }


}
